package com.lff.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lff.domain.Provider;
import com.lff.domain.User;
import com.lff.mapper.ProviderMapper;
import com.lff.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("provider")
public class ProviderController {
    @Autowired
    private ProviderService providerService;

    @RequestMapping("showProvider.do")
    public ModelAndView showProvider(ModelAndView modelAndView, @RequestParam(value = "pageIndex", defaultValue = "1") Integer startPage,
                                     HttpServletRequest request) {
        //获取条件查询使用的数据 供应商编码和供应商名称
        String queryProCode = request.getParameter("queryProCode");
        String queryProName = request.getParameter("queryProName");
        //设置分页信息
        PageHelper.startPage(startPage, 5);
        //执行查询操作
        List<Provider> providerList = providerService.findAll(queryProCode, queryProName);

        //获取分页信息
        PageInfo<Provider> pageInfo = new PageInfo<Provider>(providerList);
        //填充数据给页面
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.addObject("providerList", providerList);

        //填充用于回显到表单的数据
        modelAndView.addObject("queryProCode", queryProCode);
        modelAndView.addObject("queryProName", queryProName);
        //设置视图
        modelAndView.setViewName("providerlist");
        //返回模型对象
        return modelAndView;
    }

    /**
     * 查看用户详情信息
     */

    @RequestMapping("showProv.do")
    @Transactional
    public String showProv(Model model, @RequestParam("proid") Long id) {
        Provider provider = providerService.getProvider(id);
        model.addAttribute("provider", provider);
        return "providerview";
    }

    /**
     * 修改用户。。 页面展示
     */

    @RequestMapping("modifyProv")

    public String modifyProv(Model model, @RequestParam("proid") Long id) {
        Provider provider = providerService.getProvider(id);
        model.addAttribute("provider", provider);
        return "providermodify";
    }

    //操作执行
    @RequestMapping("modifyProv.do")
    @Transactional
    public String doModifyProv(Model model, Provider provider, HttpSession session) {
        //设置修改时间
        provider.setModifyDate(new Date());
        //设置修改人
        User user = (User) session.getAttribute("user");
        provider.setModifyBy(user);
        try {
            providerService.updateProv(provider);
        } catch (Exception e) {
            return "redirect:showProvider.do";
        }
        //跳转查看页面  让用户查看详细信息
        return showProv(model,provider.getId());
    }


    /**
     * 显示用户添加页面
     */
    @RequestMapping("addProv")
    public String addProv(){
        return "provideradd";
    }
    /*
    执行修改操作
     */
    @RequestMapping("addProv.do")
    @Transactional
    public String doAddProv(Provider provider,HttpSession session){
        User user = (User) session.getAttribute("user");
        //设置添加者
        provider.setCreatedBy(user);
        //设置添加时间
        provider.setCreationDate(new Date());
        //执行添加操作
        System.out.println(provider);
        providerService.save(provider);
        return "redirect:showProvider.do";
    }


    //删除
    @RequestMapping("/delProv.do")
    @ResponseBody
    public String delProv(@RequestParam("proid") Long id){
        Integer result = providerService.delProv(id);
        String delResult = null;
        try {
            if (result == 0) {
                delResult = "notexist";
            } else {
                delResult = "true";
            }
        } catch (Exception e) {
            delResult = "false";
            return "{\"delResult\":\"" + delResult + "\"}";
        }
        return "{\"delResult\":\"" + delResult + "\"}";
    }
}
